package com.yx.yinshanjinniu.activitys;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.adapters.HeaderSelectAdapter;
import com.yx.yinshanjinniu.application.MyApplication;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.base.BaseRVAdapter;
import com.yx.yinshanjinniu.bean.HeaderSelectBean;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class HeaderSelectActivity extends BaseActivity {

    private static final String TAG = "HeaderSelectActivity";
    private RecyclerView header_select_rv;
    private HeaderSelectAdapter adapter;
    private List list = new ArrayList();
    @Override
    protected int setContentView() {
        return R.layout.activity_header_select;
    }

    @Override
    protected void findViews() {
        header_select_rv = $(R.id.header_select_rv);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(
                4, StaggeredGridLayoutManager.VERTICAL);
        header_select_rv.setLayoutManager(layoutManager);
        adapter = new HeaderSelectAdapter(mContext,list);
        header_select_rv.setAdapter(adapter);
        getListData();

        adapter.setOnItemClickListener(new BaseRVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(List data, View view, int position, long id) {
                HeaderSelectBean  bean =  (HeaderSelectBean)list.get(position);
                editHeaderImage(bean.getValue()+"");
            }
        });
    }
    private void editHeaderImage(String header_id){
        mAppAction = MyApplication.getApplication().getAppAction();
        mAppAction.editUserAvatar(header_id, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                ToastUtil.showToast(mContext,"操作成功");
                finish();
            }
            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext,message);
            }
        });
    }
    @Override
    protected void setListeners() {

    }
    void  getListData(){
        mAppAction = MyApplication.getApplication().getAppAction();
        mAppAction.getAvatarList(new ActionCallbackListener<List<HeaderSelectBean>>() {
            @Override
            public void onSuccess(List<HeaderSelectBean> data) {
                list.clear();
                list.addAll(data);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext,message);
            }
        });
    }
}
